//
//  KuKu_Plist.swift
//  kuku
//
//  Created by 清风徐来 on 2024/1/27.
//

import UIKit
import Foundation

@objc open class KuKu_Plist: NSObject {

   public typealias InfoPlistCheckResultBlock = (_ success: Bool, _ message: String) -> Void

   @objc public static func checkInfoPlistKeys(block: InfoPlistCheckResultBlock?) {
        guard let block = block else { return }
        let sdkBundle = Bundle(for: KuKu_Plist.self)
        let infoDict = sdkBundle.infoDictionary
        let keysToCheck = ["CFBundleURLTypes",  // URL type
                           "NSPhotoLibraryUsageDescription", // 相册
                           "NSAppTransportSecurity",  // 网络
                           "LSApplicationQueriesSchemes",  // URL scheme
                           "UIViewControllerBasedStatusBarAppearance",  // 状态栏
                           "KuKu_LocalTime",
                           "KuKu_LocalScheme",
                           "KuKu_LocalLink"]
        let missingKeys = keysToCheck.filter { infoDict?[$0] == nil }
        if missingKeys.isEmpty {
            block(true, "info.plist自检通过")
        } else {
            block(false, "info.plist中缺失的Key: \(missingKeys.joined(separator: ", "))")
        }
    }

    @objc public static func loadBundleIdFromPlist() -> String? {
        let sdkBundle = Bundle(for: KuKu_Plist.self)
        return sdkBundle.bundleIdentifier
    }

    @objc public static func loadVersionFromPlist() -> String? {
        let sdkBundle = Bundle(for: KuKu_Plist.self)
        return sdkBundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
    }

    @objc public static func loadLocalTimeFromPlist() -> String? {
        let sdkBundle = Bundle(for: KuKu_Plist.self)
        return sdkBundle.infoDictionary?["KuKu_LocalTime"] as? String
    }

    @objc public static func loadSchemeFromPlist() -> String? {
        let sdkBundle = Bundle(for: KuKu_Plist.self)
        return sdkBundle.infoDictionary?["KuKu_LocalScheme"] as? String
    }

    @objc public static func loadLinkFromPlist() -> String? {
        let sdkBundle = Bundle(for: KuKu_Plist.self)
        return sdkBundle.infoDictionary?["KuKu_LocalLink"] as? String
    }

    @objc public static func getCurrentTimestampInChinaTimeZone() -> Int {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.timeZone = TimeZone(identifier: "Asia/Shanghai")

        let datenow = Date()
        let timeSp = Int(datenow.timeIntervalSince1970)
        return timeSp
    }


}
